package com.ruoyi.web.qualityController;

import cn.hutool.core.collection.IterUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.Date;
import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.quality.QualityReceipts;
import com.ruoyi.system.domain.quality.QualityReceiptsDetail;
import com.ruoyi.system.service.IQualityReceiptsDetailService;
import com.ruoyi.system.service.IQualityReceiptsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 质检单明细(任务)Controller
 *
 * @author ruoyi
 * @date 2023-03-09
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
        @Api(tags ="质检单明细(任务)")
    @RestController
@RequestMapping("/quality/detail" )
public class QualityReceiptsDetailController extends BaseController {

    private final IQualityReceiptsDetailService iQualityReceiptsDetailService;


    @Autowired
    private TokenService tokenService;
    @Autowired
    private IQualityReceiptsService iQualityReceiptsService;


    /**
     * 查询质检单明细(任务)列表
     */
    //@PreAuthorize("@ss.hasPermi('quality:detail:list')")
    @GetMapping("/listByReceiptsId/{ReceiptsId}")
        @ApiOperation(value = "查询质检单明细(任务)列表通过质检单")
        public TableDataInfo list(@PathVariable("ReceiptsId")String ReceiptsId)
    {
        LambdaQueryWrapper<QualityReceiptsDetail> lqw = new LambdaQueryWrapper<QualityReceiptsDetail>();
        lqw.eq(QualityReceiptsDetail::getReceiptsId,ReceiptsId);
        List<QualityReceiptsDetail> list = iQualityReceiptsDetailService.listParam(lqw);
        return getDataTable(list);
    }

//    /**
//     * 查询质检单明细(任务)列表
//     */
//    //@PreAuthorize("@ss.hasPermi('quality:detail:list')")
//    @GetMapping("/list")
//    @ApiOperation(value = "查询质检单明细(任务)列表")
//    public TableDataInfo list(QualityReceiptsDetail qualityReceiptsDetail)
//    {
//        startPage();
//        LambdaQueryWrapper<QualityReceiptsDetail> lqw = new LambdaQueryWrapper<QualityReceiptsDetail>();
//        if (qualityReceiptsDetail.getStatus() != null){
//            lqw.eq(QualityReceiptsDetail::getStatus ,qualityReceiptsDetail.getStatus());
//        }
//        if (qualityReceiptsDetail.getCheckUser() != null){
//            lqw.eq(QualityReceiptsDetail::getCheckUser ,qualityReceiptsDetail.getCheckUser());
//        }
//        if (qualityReceiptsDetail.getReceiptsId() != null){
//            lqw.eq(QualityReceiptsDetail::getReceiptsId ,qualityReceiptsDetail.getReceiptsId());
//        }
//        if (StringUtils.isNotBlank(qualityReceiptsDetail.getRemark())){
//            lqw.like(QualityReceiptsDetail::getRemark ,qualityReceiptsDetail.getRemark());
//        }
//        if (qualityReceiptsDetail.getProjectId() != null){
//            lqw.eq(QualityReceiptsDetail::getProjectId ,qualityReceiptsDetail.getProjectId());
//        }
//        if (qualityReceiptsDetail.getTypeId() != null){
//            lqw.eq(QualityReceiptsDetail::getTypeId ,qualityReceiptsDetail.getTypeId());
//        }
//        List<QualityReceiptsDetail> list = iQualityReceiptsDetailService.listParam(lqw);
//        return getDataTable(list);
//    }

//    /**
//     * 导出质检单明细(任务)列表
//     */
//    //@PreAuthorize("@ss.hasPermi('quality:detail:export')" )
//    @Log(title = "质检单明细(任务)" , businessType = BusinessType.EXPORT)
//    @GetMapping("/export" )
//                @ApiOperation(value = "导出质检单明细(任务)列表")
//            public AjaxResult export(QualityReceiptsDetail qualityReceiptsDetail) {
//        LambdaQueryWrapper<QualityReceiptsDetail> lqw = new LambdaQueryWrapper<QualityReceiptsDetail>(qualityReceiptsDetail);
//        List<QualityReceiptsDetail> list = iQualityReceiptsDetailService.list(lqw);
//        ExcelUtil<QualityReceiptsDetail> util = new ExcelUtil<QualityReceiptsDetail>(QualityReceiptsDetail. class);
//        return util.exportExcel(list, "detail" );
//    }

//    /**
//     * 获取质检单明细(任务)详细信息
//     */
//    //@PreAuthorize("@ss.hasPermi('quality:detail:query')" )
//    @GetMapping(value = "/{id}" )
//                @ApiOperation(value = "获取质检单明细(任务)详细信息")
//            public AjaxResult getInfo(@PathVariable("id" ) Long id) {
//        return AjaxResult.success(iQualityReceiptsDetailService.getById(id));
//    }

//    /**
//     * 新增质检单明细(任务)
//     */
//    //@PreAuthorize("@ss.hasPermi('quality:detail:add')" )
//    @Log(title = "质检单明细(任务)" , businessType = BusinessType.INSERT)
//    @PostMapping
//                @ApiOperation(value = "新增质检单明细(任务)")
//            public AjaxResult add(@RequestBody QualityReceiptsDetail qualityReceiptsDetail) {
//        return toAjax(iQualityReceiptsDetailService.save(qualityReceiptsDetail) ? 1 : 0);
//    }
//

    /**
     * 修改质检单明细(任务)
     */
    //@PreAuthorize("@ss.hasPermi('quality:detail:edit')" )
    @Log(title = "质检单明细(任务) result" , businessType = BusinessType.UPDATE)
    @PutMapping("/{result}/{receiptsId}")
                @ApiOperation(value = "修改质检单明细(任务)  未填0 不合格1 合格2")
            public AjaxResult edit(@RequestBody List<QualityReceiptsDetail> list,@PathVariable("result")String result,@PathVariable Long receiptsId) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        QualityReceipts q=new QualityReceipts();

        if(IterUtil.isNotEmpty(list)){
            list.stream().forEach(i->{
                i.setCheckUser(loginUser.getUser().getUserId());
                i.setFinishTime(new Date());
                iQualityReceiptsDetailService.updateById(i);
            });
        }

        q.setId(receiptsId);

        if(result.equals("1")){
            q.setStatus(1l);
        }else if(result.equals("2")){
            q.setStatus(2l);
        }else {
            q.setStatus(0l);
        }

        iQualityReceiptsService.updateById(q);
        return toAjax(true? 1 : 0);
    }

//    /**
//     * 删除质检单明细(任务)
//     */
//    //@PreAuthorize("@ss.hasPermi('quality:detail:remove')" )
//    @Log(title = "质检单明细(任务)" , businessType = BusinessType.DELETE)
//    @DeleteMapping("/{ids}" )
//                @ApiOperation(value = "删除质检单明细(任务)")
//            public AjaxResult remove(@PathVariable Long[] ids) {
//        return toAjax(iQualityReceiptsDetailService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
//    }
}
